package com.qf.controller;

import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.utils.BaseResponse;
import com.qf.utils.ErrorCode;
import com.qf.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author zdl
 * @date 2025/2/7
 */
//@Controller
@RestController
@ResponseBody
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody User user, HttpSession session){
//        System.out.println(user.getUsername());
//        System.out.println(user.getPassword());
        //设置session
        session.setAttribute("username",user);
        User user1 = userService.login(user);
        if (user1 == null){
            session.removeAttribute("username");
            return ResultUtils.error(ErrorCode.USER_NAME_PASSWORD_ERROR);
        }
        return ResultUtils.success(user1);
    }

    /**
     * 注销
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse logout(HttpSession session){
        if (session != null){
            session.removeAttribute("username");
            return ResultUtils.success(200,"注销成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR);
    }

    @GetMapping("/user")
    public String getUser() {
        userService.findUser();
        return "ok.html";
    }

    @GetMapping("/del")
    public String delUser() {
        userService.delUser();
        return "ok.html";
    }


}
